﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HUGATE.ItemObject;
using CoreLibs;
using HUGATE.Utility;

/// <summary>
///  Creator : Nguyễn Đức Hiếu
/// Date    : 24/12/2010
/// Desc    : Quản lý giá trị sản phẩm
/// </summary>
namespace HUGATE.Item.Controls.ProductValue
{
    public partial class ProductValueEdit : System.Web.UI.UserControl
    {
        #region Variables

        private string strListPage = "ProductValue/ProductValue.aspx";
        private int intValueID = 0;

        #endregion

        #region Properties

        public string ListPage
        {
            get { return strListPage; }
            set { strListPage = value; }
        }

        public int ValueID
        {
            get { return intValueID; }
            set { intValueID = value; }
        }

        #endregion

        #region Permissions

        //String strAddPermission = "ITEM_Unit_Add";
        //String strEditPermission = "ITEM_Unit_Edit";
        //String strDeletePermission = "ITEM_Unit_Delete";

        #endregion

        #region Support Functions

        /// <summary>
        /// Reset Control's data
        /// </summary>
        private void Clear()
        {
            txtObjectID.Text = "";
            txtObjectName.Text = "";
            txtDesc.Text = "";
            chkIsDefault.Checked = false;
            chkIsActived.Checked = false;
            //chkIsSystem.Checked = false;
            EnableControl(true);
        }

        /// <summary>
        /// Enable/Disable controls
        /// </summary>
        /// <param name="bolEnable"></param>
        private void EnableControl(bool bolEnable)
        {
            txtObjectName.Enabled = bolEnable;
            txtDesc.Enabled = bolEnable;
            chkIsDefault.Enabled = bolEnable;
            chkIsActived.Enabled = bolEnable;
            chkIsActived_CheckedChanged(null, null);

            udpEdit.Update();
        }

        /// <summary>
        /// Load Object's info to controls
        /// </summary>
        public void LoadInfo()
        {
            ITEMProductValue objValue = new ITEMProductValue();
            objValue.ValueID = intValueID;
            if (objValue.LoadInfo())
            {
                txtObjectID.Text = objValue.ValueID.ToString();
                txtObjectName.Text = objValue.ValueName.Trim();
                int index = ddlAttributeID.Items.IndexOf(ddlAttributeID.Items.FindByValue(Session["IDAttribute"].ToString()));
                ddlAttributeID.SelectedIndex = index;
                txtDesc.Text = objValue.Description.Trim();
                chkIsDefault.Checked = objValue.IsDefault;
                chkIsActived.Checked = objValue.IsActived;
                //chkIsSystem.Checked = objValue.IsSystem;

                //EnableControl(!chkIsSystem.Checked);
            }
        }

        public void FillDll()
        {
            ITEMProductAttribute objAttribute = new ITEMProductAttribute();
            DataTable objdt = objAttribute.SearchData("");
            ddlAttributeID.DataTextField = "AttributeName";
            ddlAttributeID.DataValueField = "AttributeID";
            ddlAttributeID.DataSource = objdt;
            ddlAttributeID.DataBind();
        }
        #endregion

        #region Event Functions

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCancel.Text = HUGATE.Utility.ResxManager.GetString("Btn_Cancel");
                btnUpdate.Text = HUGATE.Utility.ResxManager.GetString("Btn_Save");
                txtDesc.MaxLength = 2000;
                txtObjectName.MaxLength = 200;
                FillDll();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ITEMProductValue objValue = new ITEMProductValue();

            objValue.ValueName = txtObjectName.Text.Trim();
            objValue.AttributeID = long.Parse(ddlAttributeID.SelectedValue);
            objValue.Description = txtDesc.Text.Trim();
            objValue.IsDefault = chkIsDefault.Checked;
            objValue.IsActived = chkIsActived.Checked;
            objValue.CreatedUser = "administrator";//User.UserName;

            if (txtObjectID.Text.Trim().Length > 0)
            {
                objValue.ValueID = Convert.ToInt32(txtObjectID.Text.Trim());
                if (objValue.Update())
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("UpdateItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                else
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotUpdateItem"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);
            }
            else
            {
                if (objValue.Insert())
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("InsertItemSuccessful"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.Information);
                else
                    GlobalFunction.ShowMessageBox(Page, string.Format(HUGATE.Utility.ResxManager.GetString("CouldNotInsertItem"), HUGATE.Utility.ResxManager.GetString("ITEMProductValue", true).ToLower()), HUGATE.Utility.ResxManager.GetString("MessageBox"), GlobalFunction.MessageBoxIcon.WarningMessage);
            }

            RaisePostBackEvent(e);
            Clear();
            Session["IDAttribute"] = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Clear();
            RaisePostBackEvent(e);
            Session["IDmeAttribute"] = "";
        }
        protected void chkIsActived_CheckedChanged(object sender, EventArgs e)
        {
            //mpeUpdate.Show();
            //chkIsDefault.Enabled = chkIsActived.Checked && chkIsActived.Enabled;
            //if (!chkIsActived.Checked)
            //    chkIsDefault.Checked = false;
        }

        #endregion

        #region Event Handler

        public delegate void SaveDelegate(object sender, EventArgs e);
        public event SaveDelegate SavedEvent = null;

        protected void RaisePostBackEvent(EventArgs e)
        {
            if (SavedEvent != null)
                SavedEvent(this, e);
        }

        #endregion
    }
}